/*
 * $Id: RequestDefnDAO.java,v 1.16 2009/07/29 17:22:59 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestList;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestDefn;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestType;

public class RequestDefnDAO extends BaseDAO implements IRequestDefnDAO {

	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -8494814726825180707L;
	private IRequestTypeDAO requestTypeDAO = null;

	public RequestDefnDAO() {
		super(CfgRequestDefn.TABLE_NAME);
	}

	/**
	 * Based on the request and requestList attempt to determine the request
	 * Definition. Some might argue this is business logic, but tough this is
	 * where I can do this logic so I am.
	 * 
	 * @param request
	 * @param requestList
	 * @return
	 */
	public CfgRequestDefn get(ActRequest request, ActRequestList requestList) {
		if (requestList.getRequestTypeId() == null
				|| request.getModality() == null) {
			return null;
		}
		HashMap<String, Object> queryValues = new HashMap<String, Object>();
		queryValues.put(CfgRequestDefn.COL_TYPE_ID, requestList
				.getRequestTypeId());
		queryValues.put(CfgRequestDefn.COL_MODALITY, request.getModality());
		List<?> lstObject = findByProperties(queryValues);
		/*
		 * It is technically possible that request defn is not unique if only
		 * seen at this level. It is not possible given the configuration
		 * elsewhere in the system.
		 */
		if (!ASCUtil.isNullOrEmptyList(lstObject)) {
			return (CfgRequestDefn) lstObject.get(0);
		}
		return null;
	}

	public CfgRequestDefn getByRequestDefnId(Long requestDefnId) {
		Object requestDefn = findObjectByID(CfgRequestDefn.COL_DEFN_ID,
				requestDefnId);
		if (!ASCUtil.isNullOrEmptyObject(requestDefn)) {
			return (CfgRequestDefn) requestDefn;
		}
		return null;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestDefnDAO#getRequestDefnForModality(java.lang.String,
	 *      java.lang.String)
	 */
	public List<CfgRequestDefn> getRequestDefnForModality(String modality,
			String requestTypeName) {
		CfgRequestType requestType = requestTypeDAO
				.getRequestableRequestTypeByName(requestTypeName);
		if (ASCUtil.isNullOrEmptyObject(requestType)) {
			return null;
		}
		HashMap<String, Object> queryValues = new HashMap<String, Object>();
		queryValues.put(CfgRequestDefn.COL_TYPE_ID, requestType
				.getRequestTypeId());
		queryValues.put(CfgRequestDefn.COL_MODALITY, modality);
		List<?> rtvdDefnLst = findByProperties(queryValues);
		if (ASCUtil.isNullOrEmptyList(rtvdDefnLst)) {
			return null;
		}
		List<CfgRequestDefn> requestDefnLst = new ArrayList<CfgRequestDefn>();
		for (Object requestDefn : rtvdDefnLst) {
			requestDefnLst.add((CfgRequestDefn) requestDefn);
		}
		return requestDefnLst;
	}

	public CfgRequestDefn getByModalityModelTypeAndRequestTypeId(
			String modality, String modelType, Long requestTypeId) {
		HashMap<String, Object> queryValues = new HashMap<String, Object>();
		queryValues.put(CfgRequestDefn.COL_TYPE_ID, requestTypeId);
		queryValues.put(CfgRequestDefn.COL_MODALITY, modality);
		queryValues.put(CfgRequestDefn.COL_MODEL_TYPE, modelType);
		List<?> lstObject = findByProperties(queryValues);
		if (!ASCUtil.isNullOrEmptyList(lstObject)) {
			return (CfgRequestDefn) lstObject.get(0);
		}
		return null;
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestDefnDAO#getRequestableRequestTypesPerModality()
	 */
	public Map<String, List<String>> getRequestableRequestTypesPerModality() {
		Map<String, List<String>> requestTypeModalityMap = new HashMap<String, List<String>>();
		List<?> rtvdLst = getHibernateTemplate().find(
				this.getRequestTypesPerModalityQueryString(),
				new Object[] { "Y" });
		if (ASCUtil.isNullOrEmptyList(rtvdLst)) {
			return requestTypeModalityMap;
		}
		for (Object o : rtvdLst) {
			Object[] list = (Object[]) o;
			String modality = (String) list[0];
			String requestTypeName = (String) list[1];
			List<String> requestTypeLst = requestTypeModalityMap.get(modality);
			if (ASCUtil.isNullOrEmptyObject(requestTypeLst)) {
				requestTypeLst = new ArrayList<String>();
			}
			requestTypeLst.add(requestTypeName);
			requestTypeModalityMap.put(modality, requestTypeLst);
		}
		return requestTypeModalityMap;
	}

	/**
	 * Creates the query string
	 * 
	 * @return
	 */
	public String getRequestTypesPerModalityQueryString() {
		return new StringBuffer().append("select distinct ").append(
				"requestDefn.").append(CfgRequestDefn.COL_MODALITY)
				.append(", ").append("requestType.").append(
						CfgRequestType.COL_REQUEST_TYPE_NAME).append(" from ")
				.append(CfgRequestDefn.TABLE_NAME).append(" as requestDefn")
				.append(", ").append(CfgRequestType.TABLE_NAME).append(
						" as requestType where ").append("requestType.")
				.append(CfgRequestType.COL_IS_REQUESTABLE).append(
						"=? and requestType.").append(
						CfgRequestType.COL_REQUEST_TYPE_ID).append(
						" = requestDefn.").append(CfgRequestDefn.COL_TYPE_ID)
				.append(" group by ").append("requestDefn.").append(
						CfgRequestDefn.COL_MODALITY).append(", ").append(
						"requestType.").append(
						CfgRequestType.COL_REQUEST_TYPE_NAME).append(
						" order by ").append("requestDefn.").append(
						CfgRequestDefn.COL_MODALITY).append(", ").append(
						"requestType.").append(
						CfgRequestType.COL_REQUEST_TYPE_NAME).append(" asc")
				.toString();
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}

	public void deleteByProperty(String propertyName, Object value,
			String tableName) {
		super.deleteByProperty(propertyName, value, tableName);
	}

	/**
	 * @param requestTypeDAO
	 *            the requestTypeDAO to set
	 */
	public void setRequestTypeDAO(IRequestTypeDAO requestTypeDAO) {
		this.requestTypeDAO = requestTypeDAO;
	}
}
